package com.ysd.lis.service.lab.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.PubEmployee;
import com.ysd.lis.entity.SysUser;
import com.ysd.lis.entity.lab.LabDeliveryReceipt;
import com.ysd.lis.entity.lab.LabReqMain;
import com.ysd.lis.entity.lab.LabReqState;
import com.ysd.lis.mapper.lab.LabDeliveryReceiptMapper;
import com.ysd.lis.mapper.lab.LabReqMainMapper;
import com.ysd.lis.service.lab.LabDeliveryReceiptService;
import com.ysd.util.RedisUserManager;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 标本交接单主表 服务实现类
 * </p>
 *
 * @author bai
 * @since 2024-02-24
 */
@Service
public class LabDeliveryReceiptServiceImpl extends ServiceImpl<LabDeliveryReceiptMapper, LabDeliveryReceipt> implements LabDeliveryReceiptService {

    @Autowired
    LabDeliveryReceiptMapper labDeliveryReceiptMapper;
    @Autowired
    LabDeliveryReceiptService deliveryReceiptService;
    @Autowired
    LabReqMainMapper labReqMainMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result creatReceipt(LabDeliveryReceipt labDeliveryReceipt) {
        if (ToolsUtils.isEmpty(labDeliveryReceipt)) {
            return Result.fail("入参不可为空");
        }
        SysUser sysUser = RedisUserManager.getUser();
        if (ToolsUtils.isNotEmpty(labDeliveryReceipt.getBatchNo())) {
            LambdaQueryWrapper<LabDeliveryReceipt> queryStateWrapper = new LambdaQueryWrapper<>();
            queryStateWrapper.eq(LabDeliveryReceipt::getBatchNo, labDeliveryReceipt.getBatchNo()).eq(LabDeliveryReceipt::getDelFlag, 0);
            List<LabDeliveryReceipt> list = labDeliveryReceiptMapper.selectList(queryStateWrapper);
            if (ToolsUtils.isNotEmpty(list)) { //执行修改即可
                LambdaUpdateWrapper<LabDeliveryReceipt> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
                lambdaUpdateWrapper.eq(LabDeliveryReceipt::getBatchNo, labDeliveryReceipt.getBatchNo())
                        .set(LabDeliveryReceipt::getHandoverTime, new Date());
                int a = labDeliveryReceiptMapper.update(null, lambdaUpdateWrapper);
                if (a > 0) {
                    return Result.succ("成功！");
                } else {
                    return Result.succ("失败！");
                }
            }
        }
        boolean save = false;
        if (ToolsUtils.isNotEmpty(sysUser)) {
            labDeliveryReceipt.setOrgId(sysUser.getOrgId());
        }
        labDeliveryReceipt.setHandoverTime(new Date());
        //labDeliveryReceipt.setBatchNo(IncrementalNumberGenerator.generateIncrementalNumber8());
        save = this.save(labDeliveryReceipt);
        if (save) {
            return Result.succ("成功！");
        } else {
            return Result.succ("失败！");
        }
    }

    @Override
    public Result getLabDeliveryReceiptlist(LabDeliveryReceipt labDeliveryReceipt) {
        MPJLambdaWrapper<LabDeliveryReceipt> queryWrapper = new MPJLambdaWrapper<>();
        SysUser sysUser = RedisUserManager.getUser();
        if (ToolsUtils.isNotEmpty(sysUser)) {
            queryWrapper.eq(LabDeliveryReceipt::getOrgId, sysUser.getOrgId());
        }
        queryWrapper.selectAll(LabDeliveryReceipt.class);
        queryWrapper.selectAs("emp", PubEmployee::getName, LabDeliveryReceipt::getHandoverPersonNa);
        queryWrapper.selectAs("empR", PubEmployee::getName, LabDeliveryReceipt::getReceiverNa);
        queryWrapper.eq(LabDeliveryReceipt::getType, labDeliveryReceipt.getType());
        queryWrapper.eq(LabDeliveryReceipt::getDelFlag, 0);
        queryWrapper.leftJoin(PubEmployee.class, "emp", p -> p.eq(PubEmployee::getCode, LabDeliveryReceipt::getHandoverPersonId).eq(PubEmployee::getDelFlag, 0).isNotNull(PubEmployee::getCode).ne(PubEmployee::getCode, ""));
        queryWrapper.leftJoin(PubEmployee.class, "empR", p -> p.eq(PubEmployee::getCode, LabDeliveryReceipt::getReceiver).eq(PubEmployee::getDelFlag, 0).isNotNull(PubEmployee::getCode).ne(PubEmployee::getCode, ""));
        List<LabDeliveryReceipt> list = labDeliveryReceiptMapper.selectList(queryWrapper);
        return Result.succ(list);
    }

    @Override
    public Result getRptMainlistByBatchNo(LabDeliveryReceipt labDeliveryReceipt) {
        SysUser sysUser = RedisUserManager.getUser();
        MPJLambdaWrapper<LabReqMain> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(LabReqMain.class);
        queryWrapper.leftJoin(LabReqState.class, LabReqState::getReqMainId, LabReqMain::getId);
        // if(ToolsUtils.isNotEmpty(labDeliveryReceipt.getBatchNo())){
        queryWrapper.eq(LabReqState::getBatchNo, labDeliveryReceipt.getBatchNo());
        // }
        List<LabReqMain> list = labReqMainMapper.selectList(queryWrapper);
        return Result.succ(list);
    }
}
